package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.ExcelSubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listener.MySubjectEventListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.atguigu.servicebase.handler.GuliExceptionHandler;
import com.atguigu.servicebase.handler.MyException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.One;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2022-01-25
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    /**
     * 从excel里面读出数据
     */
    @Override
    public void addSubject(MultipartFile file, EduSubjectService eduSubjectService) {

        try {
            InputStream in = file.getInputStream();
            EasyExcel.read(in, ExcelSubjectData.class,new MySubjectEventListener(eduSubjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
            throw new MyException(20001,"执行添加异常失败");
        }

    }

    @Override
    public List<OneSubject> getOneTwoAllSubject() {
        //是树形一级，二级课程的集合
        List<OneSubject> OneTwoFinalSubjectList=new ArrayList<>();

        //可以用baseMapper来实现查询
        //先查找所有数据库的一级课程
        QueryWrapper<EduSubject> OneWrapper = new QueryWrapper<>();
        OneWrapper.eq("parent_id", "0");

        //在impl中可以通过baseMapper来实现数据库操作

        List<EduSubject> OneAllEduSubjects = baseMapper.selectList(OneWrapper);

        //先查找所有数据库的二级课程 parent_id !=0时
        QueryWrapper<EduSubject> TWOWrapper = new QueryWrapper<>();
        TWOWrapper.ne("parent_id", "0");
        List<EduSubject> TwoAllEduSubjects = baseMapper.selectList(TWOWrapper);

        //遍历所有数据库的一级课程 并抽出数据库的一级课程并且赋值到我们的OneSubject中
        for (int i = 0; i < OneAllEduSubjects.size(); i++) {
            //获取的每个数据库的一级课程
            EduSubject OneEduSubject = OneAllEduSubjects.get(i);
            //创建OneSubject
            OneSubject oneSubject = new OneSubject();
            //赋值
            BeanUtils.copyProperties(OneEduSubject,oneSubject);

            //获取所有的二级课程列表拼接
            List<TwoSubject> TwoFinalSubjectList = new ArrayList<>();
            for (int m = 0; m < TwoAllEduSubjects.size(); m++) {
                //获取的每个数据库的二级课程
                EduSubject twoEduSubject = TwoAllEduSubjects.get(m);
                if(twoEduSubject.getParentId().equals(OneEduSubject.getId())){
                    //创建TwoSubject
                    TwoSubject twoSubject = new TwoSubject();
                    //赋值
                    BeanUtils.copyProperties(twoEduSubject,twoSubject);

                    TwoFinalSubjectList.add(twoSubject);
                }
                oneSubject.setChildren(TwoFinalSubjectList);
            }
            OneTwoFinalSubjectList.add(oneSubject);

        }



        return OneTwoFinalSubjectList;
    }
}
